#!/usr/bin/env bash -x

# activate debugging from here
#set -x
DEBUG=0;
NumArgs=$#;

seavm="yes";
batman="yes";


function usage
{
    echo ""
    echo "IandV directories creator ver $VERSION";
    echo "Usage: $0 <dirname>";
    echo "e.g    iandv 17_ANZ21261_R15A";
    echo "";
}

function decho
{
    local a1="$1";
    if [ $DEBUG -eq 1 ]; then
        echo "$a1 ";
    fi    
}
function validate_argument
{
    decho "validate_argument: $1";
    if [ $1 = " " ]; then
        echo "no dir given";
        exit 1;
    fi
}

function validate_dirname
{
    decho "validate_dirname: $1";
    local x1="$1";
    if [ -d $x1 ]; then
        printf "$x1 exists \n";
    fi    
}

function create_needed_dirs
{
    decho "create_needed_dirs $1";
    local x1="$1";
    local dirlist;
    dirlist=( PASS FAIL );
    local elements=${#dirlist[@]};
    local ret=-1;
    if [ -d $x1 ]; then
        decho "$x1 exists";
    else
        mkdir "$x1";
    fi    
    decho "elements: $elements";
    chmod 777 "$x1";
    for (( i=0;i<$elements;i++)); do
        mkdir -p "$x1/${dirlist[${i}]}";
        ret="$?";
        if [ $ret -ne 0 ]; then
            echo "Cannot create $x1/${dirlist[${i}]}";
            break;
        fi    
        chmod 777 "$x1/${dirlist[${i}]}";
    done
    if [ "$batman" == "yes" ]; then
        mkdir -p  "$x1/BATMAN";
        chmod 777 "$x1/BATMAN";
    fi
    if [ "$seavm" == "yes" ]; then
        mkdir -p  "$x1/SEAVM";
        chmod 777 "$x1/SEAVM";
    fi
    printf "Created dirs successfully! \n"
}
VERSION="20110125";
MAXARG=1;

decho "Debug enabled";
decho "Number of arguments passed: $#";
if [ $NumArgs -eq 0 ]
then
    usage;
    exit 1;
fi

curdir=`pwd`
if [ ! -w $curdir ]; then
    echo "Failed to check current dir is writable";
    echo "Make sure current dir can be written";
    exit 1;
fi

dirname="$1";
validate_argument $dirname;
validate_dirname $dirname;

# APZ level
level=${dirname:0:2};
if (("$level" < "17")); then
    # No need batman dir
    batman="no";

    # No need seavm dir
    seavm="no";
else
    # find ANZ substring in dirname
    anz=( 21240 212_40 21250 212_50 21255 212_55 21260 212_60 21261 212_61 );
    e=${#anz[@]};
    for (( i=0;i<$e;i++)); do
        if [[ $dirname == *${anz[${i}]}* ]]; then
            
            case "${anz[${i}]}" in
                # Preparing for batman 40 in future
                21240 | 212_40 )
                    seavm="no";
                    batman="no";
                ;;
                21250 | 212_50 )
                    seavm="no";
                    batman="no";
                ;;
                21255 | 212_55 )
                    if (("$level" > "20")); then
                        printf "We only have 21255 system for apz20 and earlier \n";
                        printf "Sorry! Check again \n";
                        exit 0;
                    fi
                    seavm="no";
                ;;
                21260 | 212_60 | 21261 | 212_61 )
                    seavm="yes";
                    batman="yes";
                ;;
            esac
        fi
        
    done
    
fi 
#exit 1;

create_needed_dirs $dirname;

# stop debugging from here
#set +x
exit 0;

